package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Notice;
import com.example.mapper.NoticeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeService {
    @Resource
    private NoticeMapper noticeMapper;

    /**
     * 查询全部
     * @param notice
     */
    public List<Notice> selectAll(Notice notice){
        return noticeMapper.selectAll(notice);
    }

    /**
     * 分页查询+模糊匹配
     *
     * @param notice
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo selectPage(Notice notice, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Notice> list = noticeMapper.selectAll(notice);
        return PageInfo.of(list);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Notice selectById(Integer id) {
        return noticeMapper.selectById(id);
    }

    /**
     * 新增
     *
     * @param notice
     */
    public void add(Notice notice) {
        notice.setTime(DateUtil.now());
        noticeMapper.add(notice);
    }

    /**
     * 编辑
     *
     * @param notice
     */
    public void update(Notice notice) {
        notice.setTime(DateUtil.now());
        noticeMapper.update(notice);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(Integer id) {
        noticeMapper.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            noticeMapper.delete(id);
        }
    }
}
